package org.shana.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.shana.shortlink.admin.common.convention.result.Result;
import org.shana.shortlink.admin.common.convention.result.Results;
import org.shana.shortlink.admin.dto.req.UserLoginReqDTO;
import org.shana.shortlink.admin.dto.req.UserRegisterReqDTO;
import org.shana.shortlink.admin.dto.req.UserUpdateReqDTO;
import org.shana.shortlink.admin.dto.resp.UserActualRespDTO;
import org.shana.shortlink.admin.dto.resp.UserLoginRespDTO;
import org.shana.shortlink.admin.dto.resp.UserRespDTO;
import org.shana.shortlink.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/short-link/admin/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

//    @GetMapping("/{username}")
//    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
//        return Results.success(userService.getUserByUsername(username));
//    }

    /**
     * 根据用户名查询无脱敏用户信息
     */
    @GetMapping("/actual/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

    @GetMapping("/has-username")
    public Result<Boolean> hasUsername(@RequestParam String username){
        return Results.success(userService.hasUsername(username));
    }

    @PostMapping("/register")
    public  Result<String> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO){
            userService.register(userRegisterReqDTO);
            return Results.success("注册成功");
    }


    /**
     * 修改用户
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success("修改成功");
    }


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam),"登录成功");
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        Boolean login = userService.checkLogin(username, token);
        return Results.success(login,"用户已登录");
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/logout")
    public Result<String> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.success("退出登录成功");
    }
}
